package com.aduerr.v1shouyin.mvp.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.view.*;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.pedant.SweetAlert.SweetAlertDialog;
import com.aduerr.v1shouyin.App;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.common.Constants;
import com.aduerr.v1shouyin.entity.MemberAddBalanceEntity;
import com.aduerr.v1shouyin.entity.MemberGetChangeSetListEntity;
import com.aduerr.v1shouyin.entity.MemberInfoEntity;
import com.aduerr.v1shouyin.entity.MemberRechargeAndConsumeEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.util.DividerGridItemDecoration;
import com.aduerr.v1shouyin.util.LogUtils;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.aduerr.v1shouyin.view.SecurityCodeView;
import com.bumptech.glide.Glide;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.observers.BlockingBaseObserver;
import io.reactivex.schedulers.Schedulers;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;


public class MemberInchargActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.imageview_icon)
    CircleImageView imageviewIcon;
    @BindView(R.id.tv_vip_name)
    TextView tvVipName;
    @BindView(R.id.tv_phone_card)
    TextView tvPhoneCard;
    @BindView(R.id.tv_balance_yuer)
    TextView tvBalanceYuer;
    @BindView(R.id.tv_discount_card)
    TextView tvDiscountCard;
    @BindView(R.id.rv_incharg_card)
    RecyclerView rvInchargCard;
    @BindView(R.id.rl_other_money)
    RelativeLayout rlOtherMoney;
    @BindView(R.id.rb_scan)
    RadioButton rbScan;
    @BindView(R.id.rb_cash)
    RadioButton rbCash;
    @BindView(R.id.tv_incharge)
    TextView tvIncharge;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.tv_send_money)
    TextView tvSendMoney;
    @BindView(R.id.ll_contain)
    LinearLayout llContain;

    //    @BindView(R.id.tv_cal_saoma_pay_member)
//    TextView tvCalSaomaPayMember;
//    @BindView(R.id.tv_cal_xianjin_shoukuan_member)
//    TextView tvCalXianjinShoukuanMember;
    SweetAlertDialog pDialog;
    //=================================================输入密码的控件
    Dialog dialog;
    SecurityCodeView securityCodeView;
    private MemberInfoEntity memberInfoEntity;
    private MyRecycleAdpter myRecycleAdpter;
    private List<MemberGetChangeSetListEntity.DataBean> data;
    private Boolean lastitemischecked = false;
    private String scanMemberNo;
    private String payMoney;
    private String beizhu = " ";
    private String sendmoney = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_incharg);
        ButterKnife.bind(this);
        initUi();
        initDate();
        initRecycleData();
    }

    private void initUi() {
//        tvCalSaomaPayMember.setEnabled(true);
//        tvCalXianjinShoukuanMember.setEnabled(true);
//        rlshaoma.setEnabled(true);
//        rlcash.setEnabled(true);

        tvTitle.setText("会员充值");
        rvInchargCard.setLayoutManager(new GridLayoutManager(this, 3,
                GridLayoutManager.VERTICAL, false));
        rvInchargCard.addItemDecoration(new DividerGridItemDecoration(MemberInchargActivity.this, 34, getResources().getColor(R.color.white)));
        myRecycleAdpter = new MyRecycleAdpter(MemberInchargActivity.this);
        rvInchargCard.setAdapter(myRecycleAdpter);
        myRecycleAdpter.setOnItemClickListener(new MyRecycleAdpter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //更改背景颜色
                rlOtherMoney.setVisibility(View.GONE);
                ChangeBacground(position);
                if(myRecycleAdpter != null) {
                    data = myRecycleAdpter.data;
                    if(data != null) {
                        lastitemischecked = false;
                        payMoney = data.get(position).getMoney();
                        sendmoney = data.get(position).getSendmoney();

                        // 自定义金额选中了最后一个=============================
//                        if (data.get(position).getMoney().equals("其他金额")) {
//                            lastitemischecked = true;
//                            Intent intent = new Intent(MemberInchargActivity.this,
//                            MemberInchargSeconActivity.class);
////                            intent.putExtra(Constants.GOTO_CALCULATOR_TYPE,
// Constants.FROMMEMBER_TO_RECHARGE_CALCULATOR);
//                            intent.putExtra("beizhu", beizhu);
//                            intent.putExtra(Constants.SCANMEMBERNO, scanMemberNo);
//                            if (memberInfoEntity.getData() != null) {
//                                intent.putExtra("beformemberincahrgeinfo",
//                                memberInfoEntity.getData());
//                            }
//                            intent.putExtra("memberincharge", true);
//                            startActivity(intent);
//                        } else {
//                            lastitemischecked = false;
//                            payMoney = data.get(position).getMoney();
//                            sendmoney = data.get(position).getSendmoney();
//                        }
                        //==============自定义金额选中了最后一个=============================


                    }

//                    if (data.size() - 1 == position) {
//                        Intent intent = new Intent(context, CalculatorActivity.class);
//                        intent.putExtra(Constants.GOTO_CALCULATOR_TYPE, Constants
//                        .FROMMEMBER_TO_RECHARGE_CALCULATOR);
//                        intent.putExtra(Constants.SCANMEMBERNO, scanMemberNo);
//                        startActivity(intent);
//                    } else {
//                        payMoney = data.get(position).getMoney();
//                    }
                }

            }
        });

    }

    private void ChangeBacground(int position) {
        if(myRecycleAdpter != null) {
            myRecycleAdpter.setBackground(position);
        }

    }

    private void initRecycleData() {
        HashMap<String, String> addParams = new HashMap<>();
        APIRetrofit.getAPIService().memberGetChangeSetList(RXRequest.getXingPosParams(addParams, this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BlockingBaseObserver<MemberGetChangeSetListEntity>() {
                    @Override
                    public void onNext(MemberGetChangeSetListEntity memberGetChangeSetListEntity) {
                        //                   if (Tools.isAvailable
                        //                   (memberGetChangeSetListEntity))return;
                        int success = memberGetChangeSetListEntity.getSuccess();
                        if(success == 1) {
                            List<MemberGetChangeSetListEntity.DataBean> data =
                                    memberGetChangeSetListEntity.getData();
                            int size = data.size();
                            if(size > 0) {
                                if(size > 5) {
                                    List<MemberGetChangeSetListEntity.DataBean> datenew = new ArrayList<MemberGetChangeSetListEntity.DataBean>();
                                    for(int i = 0; i < 5; i++) {
                                        data.get(i).setIschecked(false);
                                        datenew.add(data.get(i));
                                    }
                                    myRecycleAdpter.setData(datenew);
                                } else {
                                    for(int i = 0; i < size; i++) {
                                        data.get(i).setIschecked(false);
                                    }
                                    myRecycleAdpter.setData(data);
                                }

//                                for (MemberGetChangeSetListEntity.DataBean bean :
//                                data) {
//                                        bean.setIschecked(false);
//                                    }
//                                myRecycleAdpter.setData(data);
                            }
                            //======================自定义充值金额去掉了============================
//                            if (size > 0) {
//                                //新建一个数据用来显示其他金额的
//                                MemberGetChangeSetListEntity.DataBean dataBean = new
//                                MemberGetChangeSetListEntity.DataBean();
//                                dataBean.setIschecked(false);
//                                dataBean.setMoney("其他金额");
//                                dataBean.setSendmoney("0");
//                                if (size < 5) {
//                                    for (MemberGetChangeSetListEntity.DataBean bean :
//                                    data) {
//                                        bean.setIschecked(false);
//                                    }
//                                    //添加一个数据
//                                    data.add(dataBean);
//                                    myRecycleAdpter.setData(data);
//                                } else {//只取前5个
//                                    List<MemberGetChangeSetListEntity.DataBean> data5
//                                    = new ArrayList<>();
//                                    for (int i = 0; i < 5; i++) {
//                                        data5.add(data.get(i));
//                                        data5.get(i).setIschecked(false);
//                                    }
//                                    data5.add(dataBean);
//                                    myRecycleAdpter.setData(data5);
//                                }
//                            }
                            //======================自定义充值金额去掉了============================
                        } else if(success == -100 | success == -200) {
                            //// TODO: 2017/12/9 0009 绑定guid  界面
                            SharedPreferencesUtils.remove(MemberInchargActivity.this,
                                    "autologin");
                            SharedPreferencesUtils.remove(MemberInchargActivity.this,
                                    "remberpassword");
                            startActivity(new Intent(MemberInchargActivity.this,
                                    BangGuidActivity.class));
                            finish();
                        } else if(success == -300 || success == -2 || success == -3 || success == -6) {
                            //// TODO: 2017/12/9 0009 去登录的界面
                            com.blankj.utilcode.util.ToastUtils.showShortSafe(memberGetChangeSetListEntity.getErrMsg());
                            errorClose();
                        } else {
                            ToastUtils.showToast(MemberInchargActivity.this,
                                    memberGetChangeSetListEntity.getErrMsg() + "");

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(e instanceof SocketTimeoutException) {
                            Toast.makeText(App.getApp(), "网络连接超时", Toast.LENGTH_SHORT).show();
                        } else if(e instanceof ConnectException) {
                            Toast.makeText(App.getApp(), "连接失败", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.e("onError:", "error:" + e.getMessage());//R.string
                            // .checknet +
                            Toast.makeText(App.getApp(), "检查网络状态", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void errorClose() {
//        SharedPreferencesUtils.remove(MemberInchargActivity.this, "autologin");
//        SharedPreferencesUtils.remove(MemberInchargActivity.this, "remberpassword");
//        startActivity(new Intent(MemberInchargActivity.this, LoginActivity.class));
//        finish();
        dialog.dismiss();
    }

    private void initDate() {
        Intent intent = getIntent();
        if(intent != null) {
            // intent.putExtra("scanMemberNo", erWeiMaResult);
            scanMemberNo = intent.getStringExtra("scanMemberNo");
            beizhu = intent.getStringExtra("beizhu");

            memberInfoEntity = (MemberInfoEntity) intent.getSerializableExtra(
                    "memberinfo");
            tvPhoneCard.setText("ID：" + memberInfoEntity.getData().getNO());
            tvVipName.setText(memberInfoEntity.getData().getNames());
            tvBalanceYuer.setText("￥" + memberInfoEntity.getData().getBalance());
            tvDiscountCard.setText(memberInfoEntity.getData().getLevelName() + "  " + memberInfoEntity.getData().getDiscount());
            Glide.with(MemberInchargActivity.this).load(memberInfoEntity.getData().getHeadImgUrl()).into(imageviewIcon);
        }
    }

    @OnClick({R.id.img_back, R.id.tv_other_money, R.id.rb_scan, R.id.rb_cash,
            R.id.tv_incharge})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_other_money:
                rlOtherMoney.setVisibility(View.VISIBLE);
                payMoney = null;
                if(myRecycleAdpter != null) {
                    myRecycleAdpter.resetColor();
                }
                break;
            case R.id.rb_scan:
                if(rbScan.isChecked()) {
                    tvIncharge.setText("扫一扫");
                }
                break;
            case R.id.rb_cash:
                if(rbCash.isChecked()) {
                    tvIncharge.setText("确认充值");
                }
                break;
            case R.id.tv_incharge:
                if(rbScan.isChecked()) {
                    if(getPayMoney() == null) {
                        ToastUtils.showToast(MemberInchargActivity.this, "请选择充值的金额");
                        return;
                    }
                    if(lastitemischecked) {
                        return;
                    } else {//0  扫码  1 现金
//                    tvCalSaomaPayMember.setEnabled(false);
//                        rlshaoma.setEnabled(true);

                        toDialogActivity(0);
                    }
                } else if(rbCash.isChecked()) {
                    if(getPayMoney() == null) {
                        ToastUtils.showToast(MemberInchargActivity.this, "请选择充值的金额");
                        return;
                    }
                    if(lastitemischecked) {
                        return;
                    } else {
//                    tvCalXianjinShoukuanMember.setEnabled(false);
//                        rlcash.setEnabled(true);
                        toDialogActivity(1);
                    }
                }
                break;
        }
    }

    @OnTextChanged(value = R.id.et_money, callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence charSequence, int start, int before, int count) {
        calcSendMoney();
    }

    /**
     * 计算赠送金额
     */
    private void calcSendMoney() {
        payMoney = etMoney.getText().toString();
        if(TextUtils.isEmpty(payMoney)) {
            tvSendMoney.setText("赠送0元");
            sendmoney = "0";
            return;
        }
        double pay = Double.parseDouble(etMoney.getText().toString());
        if(myRecycleAdpter.data.size() == 0) {
            tvSendMoney.setText("赠送0元");
            sendmoney = "0";
            return;
        } else {
            List<MemberGetChangeSetListEntity.DataBean> result = new ArrayList<>();
            for(MemberGetChangeSetListEntity.DataBean dataBean : myRecycleAdpter.data) {
                if(Double.parseDouble(dataBean.getMoney()) <= pay) {
                    result.add(dataBean);
                }
            }
            if(result.size() == 0) {
                tvSendMoney.setText("赠送0元");
                sendmoney = "0";
                return;
            }
            Collections.sort(result, (databean1, databean2) -> {
                double diff =
                        Double.parseDouble(databean1.getMoney()) - Double.parseDouble(databean2.getMoney());
                if(diff > 0) {
                    return -1;
                } else if(diff < 0) {
                    return 1;
                }
                return 0; //相等为0
            });
            MemberGetChangeSetListEntity.DataBean current = result.get(0);
            double tMoney = Double.parseDouble(current.getMoney());
            double tSentMoney = Double.parseDouble(current.getSendmoney());
            int multiple = new Double(pay / tMoney).intValue();
            int sentResult = new Double(multiple * tSentMoney).intValue();
            tvSendMoney.setText("赠送" + sentResult + "元");
            sendmoney = sentResult + "";
        }
    }

    private String getPayMoney() {
        if(payMoney == null) {
            if(TextUtils.isEmpty(etMoney.getText().toString())) {
                return null;
            } else {
                return etMoney.getText().toString();
            }
        } else {
            return payMoney;
        }
    }

    private void toDialogActivity(int type) {
        View view =
                LayoutInflater.from(MemberInchargActivity.this).inflate(R.layout.dialog_tuikuan, null);
        // 对话框
        dialog = new Dialog(MemberInchargActivity.this);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.show();
        // 设置宽度为屏幕的宽度
        WindowManager windowManager = this.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); // 设置宽度
        dialog.getWindow().setAttributes(lp);
        dialog.getWindow().setContentView(view);
        //==================================弹出键盘================================================================
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        //===========================================================================================================
//        InputTools.KeyBoard(et_hexiao, "open");
//        InputTools.HideKeyboard(et_hexiao);
        //findview
        //找到自定义的控件用来显示密码
        securityCodeView = (SecurityCodeView) view.findViewById(R.id.scv_edittext);
        TextView dialogui_tv_title = (TextView) view.findViewById(R.id.dialogui_tv_title);
        dialogui_tv_title.setText("请输入充值密码");
        securityCodeView.setInputCompleteListener(new SecurityCodeView.InputCompleteListener() {
            @Override
            public void inputComplete() {
                dialog.hide();
                LogUtils.e("getEditContent", securityCodeView.getEditContent());
                if(type == 0) {
                    toMemberAddBalance(scanMemberNo, securityCodeView.getEditContent());
                } else {
                    toCashMoneymember(scanMemberNo, securityCodeView.getEditContent());
                }

//                getPresenter().toTuikuan(MemberInchargActivity.this, securityCodeView
//                .getEditContent(), orderid_content, handinputmoney);

            }

            @Override
            public void deleteContent(boolean isDelete) {
                //DO NO THING
            }
        });
    }


    //扫码支付
    private void toMemberAddBalance(String scanMemberNo, String operatePass) {
        if(getPayMoney() == null || TextUtils.isEmpty(getPayMoney())) return;
        if(TextUtils.isEmpty(scanMemberNo)) return;
        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("scanMemberNo", scanMemberNo);
        addParams.put("payMoney", getPayMoney());
        addParams.put("operatePass", operatePass);
        addParams.put("bz", beizhu);
        APIRetrofit.getAPIService().memberAddBalance(RXRequest.getXingPosParams(addParams, MemberInchargActivity.this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MemberAddBalanceEntity>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        if(pDialog == null) {
                            pDialog = new SweetAlertDialog(MemberInchargActivity.this,
                                    SweetAlertDialog.PROGRESS_TYPE);
                            pDialog.getProgressHelper().setBarColor(Color.parseColor(
                                    "#005fee"));
                            pDialog.setTitleText("正在付款中");
                            pDialog.setCancelable(false);
                            pDialog.show();
                        }
                    }

                    @Override
                    public void onNext(MemberAddBalanceEntity memberAddBalanceEntity) {
                        //                    if (Tools.isAvailable
                        //                    (memberAddBalanceEntity)) return;
                        int success = memberAddBalanceEntity.getSuccess();
                        if(success == 1) {
                            Intent intent = new Intent(MemberInchargActivity.this,
                                    ScanningActivity.class);
                            intent.putExtra(Constants.QRCODE_CAPUTER,
                                    Constants.MEMBER_SCANCODECOLLECTION_QR);
                            intent.putExtra(Constants.REALMONEY, getPayMoney());
                            intent.putExtra("sendmoney", sendmoney);
                            intent.putExtra("beizhu", beizhu);
                            intent.putExtra("orderid", memberAddBalanceEntity.getData().getOrderid());
                            //会员的条形码的号码
                            intent.putExtra("scanMemberNo", scanMemberNo);
                            intent.putExtra(Constants.FROMORDERID,
                                    memberAddBalanceEntity.getData().getOrderid());
//                            intent.putExtra("aftermemberincahrgeinfo", data);
                            if(memberInfoEntity.getData() != null) {
                                intent.putExtra("beformemberincahrgeinfo",
                                        memberInfoEntity.getData());
                            }
                            startActivity(intent);
                            finish();
                        } else if(success == -100 | success == -200) {
                            //// TODO: 2017/12/9 0009 绑定guid  界面
                            SharedPreferencesUtils.remove(MemberInchargActivity.this,
                                    "autologin");
                            SharedPreferencesUtils.remove(MemberInchargActivity.this,
                                    "remberpassword");
                            startActivity(new Intent(MemberInchargActivity.this,
                                    BangGuidActivity.class));
                            finish();
                        } else if(success == -300 || success == -2 || success == -3 || success == -6) {
                            //// TODO: 2017/12/9 0009 去登录的界面
                            com.blankj.utilcode.util.ToastUtils.showShortSafe(memberAddBalanceEntity.getErrMsg());
                            errorClose();
                        } else if(success == -3) {//充值密码错误
//                            rlshaoma.setEnabled(true);
                            ToastUtils.showToast(MemberInchargActivity.this,
                                    memberAddBalanceEntity.getErrMsg() + "");
                        } else {
                            ToastUtils.showToast(MemberInchargActivity.this,
                                    memberAddBalanceEntity.getErrMsg() + "");

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(pDialog != null) {
                            pDialog.dismiss();
                            pDialog = null;
                        }
                        if(e instanceof SocketTimeoutException) {
                            Toast.makeText(App.getApp(), "网络连接超时", Toast.LENGTH_SHORT).show();
                        } else if(e instanceof ConnectException) {
                            Toast.makeText(App.getApp(), "连接失败", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.e("onError:", "error:" + e.getMessage());//R.string
                            // .checknet +
                            Toast.makeText(App.getApp(), "检查网络状态", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onComplete() {
                        if(pDialog != null) {
                            pDialog.dismiss();
                            pDialog = null;
                        }
                    }
                });

    }

    //现金支付
    private void toCashMoneymember(String erWeiMaResult, String operatePass) {
        if(getPayMoney() == null || TextUtils.isEmpty(getPayMoney())) return;
        if(TextUtils.isEmpty(erWeiMaResult)) return;
        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("scanMemberNo", erWeiMaResult);
        addParams.put("payMoney", getPayMoney());
        addParams.put("operatePass", operatePass);
        APIRetrofit.getAPIService().memberToPayAndConsume(RXRequest.getXingPosParams(addParams, MemberInchargActivity.this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MemberRechargeAndConsumeEntity>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        if(pDialog == null) {
                            pDialog = new SweetAlertDialog(MemberInchargActivity.this,
                                    SweetAlertDialog.PROGRESS_TYPE);
                            pDialog.getProgressHelper().setBarColor(Color.parseColor(
                                    "#005fee"));
                            pDialog.setTitleText("正在付款中");
                            pDialog.setCancelable(false);
                            pDialog.show();
                        }
                    }

                    @Override
                    public void onNext(MemberRechargeAndConsumeEntity memberRechargeAndConsumeEntity) {
                        int success = memberRechargeAndConsumeEntity.getSuccess();
                        if(success == 1) {
                            MemberRechargeAndConsumeEntity.DataBean data =
                                    memberRechargeAndConsumeEntity.getData();
                            if(data != null) {
                                //// TODO: 2017/12/12 0012  充值成功
                                Intent intent = new Intent(MemberInchargActivity.this,
                                        ResultActivity.class);
                                intent.putExtra("money", getPayMoney());
                                intent.putExtra("paytype", "现金付款");
                                intent.putExtra("orderid", data.getOrderid());
                                intent.putExtra("sendmoney", sendmoney);
                                intent.putExtra("sucess", "3");
                                intent.putExtra("beizhu", beizhu);
                                intent.putExtra("aftermemberincahrgeinfo", data);
                                if(memberInfoEntity.getData() != null) {
                                    intent.putExtra("beformemberincahrgeinfo",
                                            memberInfoEntity.getData());
                                }
                                startActivity(intent);
                                finish();
                            }
                        } else if(success == -100 | success == -200) {
                            if(pDialog != null) {
                                pDialog.dismiss();
                                pDialog = null;
                            }
                            //// TODO: 2017/12/9 0009 绑定guid  界面
                            SharedPreferencesUtils.remove(MemberInchargActivity.this,
                                    "autologin");
                            SharedPreferencesUtils.remove(MemberInchargActivity.this,
                                    "remberpassword");
                            startActivity(new Intent(MemberInchargActivity.this,
                                    BangGuidActivity.class));
                            finish();
                        } else if(success == -300 || success == -2 || success == -3 || success == -6) {
                            if(pDialog != null) {
                                pDialog.dismiss();
                                pDialog = null;
                            }
                            //// TODO: 2017/12/9 0009 去登录的界面
                            com.blankj.utilcode.util.ToastUtils.showShortSafe(memberRechargeAndConsumeEntity.getErrMsg());
                            errorClose();
                        } else if(success == -3) {//充值密码错误
                            if(pDialog != null) {
                                pDialog.dismiss();
                                pDialog = null;
                            }
                            ToastUtils.showToast(MemberInchargActivity.this,
                                    memberRechargeAndConsumeEntity.getErrMsg() + "");
                        } else {
                            if(pDialog != null) {
                                pDialog.dismiss();
                                pDialog = null;
                            }
                            ToastUtils.showToast(MemberInchargActivity.this,
                                    memberRechargeAndConsumeEntity.getErrMsg() + "");
                        }

                    }

                    @Override
                    public void onError(Throwable throwable) {
                        if(pDialog != null) {
                            pDialog.dismiss();
                            pDialog = null;
                        }
                    }

                    @Override
                    public void onComplete() {
                        if(pDialog != null) {
                            pDialog.dismiss();
                            pDialog = null;
                        }
                    }
                });
    }


    static class MyRecycleAdpter extends RecyclerView.Adapter implements View.OnClickListener {
        List<MemberGetChangeSetListEntity.DataBean> data = new ArrayList<>();
        private ViewHolder viewHolder;
        private Context context;
        private OnItemClickListener mOnItemClickListener = null;

        public MyRecycleAdpter(FragmentActivity activity) {
            this.context = activity;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                          int viewType) {
            View inflate =
                    LayoutInflater.from(context).inflate(R.layout.item_member_recharge_card, parent, false);
            viewHolder = new ViewHolder(inflate);
            //将创建的View注册点击事件
            inflate.setOnClickListener(this);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            viewHolder = (ViewHolder) holder;
            viewHolder.tvChongzhi.setText(data.get(position).getMoney() + "元");
            viewHolder.tvSend.setText("送" + data.get(position).getSendmoney() + "元");
            //将position保存在itemView的Tag中，以便点击时进行获取
            viewHolder.itemView.setTag(position);
            Boolean ischecked = data.get(position).getIschecked();
            if(ischecked) {
                viewHolder.llcard.setBackground(context.getResources().getDrawable(R.drawable.shape_btn_selecter));
                viewHolder.tvChongzhi.setTextColor(Color.WHITE);
                viewHolder.tvSend.setTextColor(Color.WHITE);
            } else {
                viewHolder.llcard.setBackground(context.getResources().getDrawable(R.drawable.shape_btn_cannot));
                viewHolder.tvChongzhi.setTextColor(context.getResources().getColor(R.color.text_color6));
                viewHolder.tvSend.setTextColor(context.getResources().getColor(R.color.text_color6));
            }
        }

        @Override
        public int getItemCount() {
            return data != null ? data.size() : 0;
        }

        //同步数据
        public void setData(List<MemberGetChangeSetListEntity.DataBean> mdata) {
            this.data = mdata;
            notifyDataSetChanged();
        }

        @Override
        public void onClick(View v) {
            if(mOnItemClickListener != null) {
                //注意这里使用getTag方法获取position
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            }
        }

        //设置背景颜色
        public void setBackground(int position) {
            for(MemberGetChangeSetListEntity.DataBean dataBean : data) {
                dataBean.setIschecked(false);
            }
            data.get(position).setIschecked(true);
            notifyDataSetChanged();
        }

        public void resetColor() {
            for(MemberGetChangeSetListEntity.DataBean dataBean : data) {
                dataBean.setIschecked(false);
            }
            notifyDataSetChanged();
        }

        public void setOnItemClickListener(OnItemClickListener listener) {
            this.mOnItemClickListener = listener;
        }

        //定义 interface
        public static interface OnItemClickListener {
            void onItemClick(View view, int position);
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_chongzhi)
            TextView tvChongzhi;
            @BindView(R.id.tv_send)
            TextView tvSend;
            @BindView(R.id.ll_itme_card)
            LinearLayout llcard;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }


}
